package com.sophimp.jumphack;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * Created by chenliang on 2018/1/1.
 */
public class HackBook {


//    static final String ADB_PATH = "/Users/chenliang/Library/Android/sdk/platform-tools/adb";
    static final String ADB_PATH = "D:\\Android\\Sdk\\platform-tools\\adb.exe";

    /**
     * 弹跳系数，现在已经会自动适应各种屏幕，请不要修改。
     */
//    static final double JUMP_RATIO = 1.390f;
    static final double JUMP_RATIO = 2f;

    private static Random RANDOM = new Random();

    public static void main(String... strings) {
        String root = HackBook.class.getResource("/").getPath();
        System.out.println("root: " + root);
        File srcDir = new File(root, "img/input");
        File desDir = new File(root, "img/output");
        srcDir.mkdirs();
        System.out.println("srcDir: " + srcDir.getAbsolutePath());
        MyPosFinder2 myPosFinder = new MyPosFinder2();
        BookFinder bookFinder = new BookFinder();
        int total = 0;
        int centerHit = 0;
        double jumpRatio = 0;
        long startTime;
        for (int i = 0; i < 9000; i++) {
            try {
                startTime = System.currentTimeMillis();
                total++;
                File file = new File(srcDir, i + ".png");
                if (file.exists()) {
                    file.deleteOnExit();
                }
//                Process process = Runtime.getRuntime().exec(ADB_PATH + " shell /system/bin/screencap -p > " + file.getAbsolutePath());
                Process process = Runtime.getRuntime().exec(ADB_PATH + " shell /system/bin/screencap -p /sdcard/screenshot.png");
                process.waitFor();
                process = Runtime.getRuntime().exec(ADB_PATH + " pull /sdcard/screenshot.png " + file.getAbsolutePath());
                process.waitFor();

                System.out.println("screenshot, file: " + file.getAbsolutePath());
                BufferedImage img = ImgLoader.load(file.getAbsolutePath());
                int[] myPos = myPosFinder.find(img);
                if (myPos != null) {
                    System.out.println("find myPos, succ, (" + myPos[0] + ", " + myPos[1] + ")");
                    int[] nextBook = bookFinder.find(img, myPos);
                    System.out.println("find nextBook, succ, (" + nextBook[0] + ", " +nextBook[1] + ") step: " + i + " cost time: " + (System.currentTimeMillis() - startTime));

//                    if (nextCenter[0] < Constant.MIN_X || nextCenter[0] > Constant.MAX_X || nextCenter[1] < Constant.MIN_Y || nextCenter[1] > Constant.MAX_Y) {
//                        // 非法坐标， 退出来
//                        System.out.println("find nextCenter, invalid, (" + centerX + ", " + centerY + ")");
//                        throw new RuntimeException("invalid target");
//                    }

//                    Graphics graphics = img.getGraphics();
//                        graphics.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
                    // 识别的人物位置
//                    graphics.setColor(Color.RED);
//                    graphics.fillRect(myPos[0] - 5, myPos[1] - 5, 10, 10);

                    // 下一跳位置
//                    graphics.setColor(Color.BLUE);
//                    graphics.fillRect(nextBook[0] - 5, nextBook[1] - 5, 10, 10);

//                    File desFile = new File(desDir, file.getName());
//                    if (!desFile.exists()) {
//                        desFile.mkdirs();
//                        desFile.createNewFile();
//                    }
//                    ImageIO.write(img, "png", desFile);

                    int clickX, clickY;
                    if (nextBook[0] > myPos[0]){
                        // 右
//                        clickX = 1050 + RANDOM.nextInt(20);
//                        clickY = 1500 + RANDOM.nextInt(100);
                        clickX = 1000;
                        clickY = 1600;
                        System.out.println("右");
                    }else {
                        // 左
//                        clickX = 250 + RANDOM.nextInt(100);
//                        clickY = 1500 + RANDOM.nextInt(100);
                        clickX = 300;
                        clickY = 1600;
                        System.out.println("左");
                    }
                    // 模拟跳跃动作
//                    int distance = (int) (Math.sqrt((centerX - myPos[0]) * (centerX - myPos[0]) + (centerY - myPos[1]) * (centerY - myPos[1])) * jumpRatio);
//                    System.out.println("distance: " + distance);
//                    int pressX = 400 + RANDOM.nextInt(100);
//                    int pressY = 500 + RANDOM.nextInt(100);
                    String adbCommand = ADB_PATH + String.format(" shell input tap %d %d", clickX, clickY);
                    System.out.println(adbCommand);
                    Runtime.getRuntime().exec(adbCommand);
                } else {
                    System.err.println("find myPos, fail");
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
            try {
                // sleep 随机时间，防止上传不了成绩
//                Thread.sleep(1000 + RANDOM.nextInt(50));
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
        System.out.println("centerHit: " + centerHit + ", total: " + total);
    }

}
